package com.cirkana.viewer.data.route;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.ArrayList;
import java.util.List;

import com.cirkana.data.IDataColumnDef;
import com.cirkana.data.IDataTableDef;
import com.cirkana.data.route.IRouteDetail;
import com.cirkana.serial.bin.BinarySerializationException;
import com.cirkana.serial.bin.BinarySerializationUtil;
import com.cirkana.serial.text.TextSerializationBuilder;
import com.cirkana.viewer.data.DataColumnDef;
import com.cirkana.viewer.data.LocalDataRecord;
import com.cirkana.viewer.data.security.User;

public class RouteDetail extends LocalDataRecord implements IRouteDetail {

	protected static final String COLUMN_ROUTE_ID = "ROUTE_ID";
	protected static final String COLUMN_TITLE = "TITLE";
	protected static final String COLUMN_DESCRIPTION = "DESCRIPTION";
	protected static final String COLUMN_PREVIEW_IMAGE = "PREVIEW_IMAGE";

	public static final IDataTableDef tableDef = new RouteTableDef();

	@Override
	public Integer getId() {
		return getIntegerField(COLUMN_ROUTE_ID);
	}

	@Override
	public void setId(Integer id) {
		setIntegerField(COLUMN_ROUTE_ID, id);
	}

	@Override
	public String getTitle() {
		return getStringField(COLUMN_TITLE);
	}

	@Override
	public void setTitle(String title) {
		setStringField(COLUMN_TITLE, title);
	}

	@Override
	public String getDescription() {
		return getStringField(COLUMN_DESCRIPTION);
	}

	@Override
	public void setDescription(String description) {
		setStringField(COLUMN_DESCRIPTION, description);
	}

	@Override
	public String getPreviewImage() {
		return getStringField(COLUMN_PREVIEW_IMAGE);
	}

	@Override
	public void setPreviewImage(String previewImage) {
		setStringField(COLUMN_PREVIEW_IMAGE, previewImage);
	}

	@Override
	public void serialize(DataOutputStream out) throws BinarySerializationException {
		BinarySerializationUtil.serializeInteger(getId(), out);
		BinarySerializationUtil.serializeString(getTitle(), out);
		BinarySerializationUtil.serializeString(getDescription(), out);
		BinarySerializationUtil.serializeString(getPreviewImage(), out);
	}

	@Override
	public void deserialize(DataInputStream in) throws BinarySerializationException {
		setId(BinarySerializationUtil.deserializeInteger(in));
		setTitle(BinarySerializationUtil.deserializeString(in));
		setDescription(BinarySerializationUtil.deserializeString(in));
		setPreviewImage(BinarySerializationUtil.deserializeString(in));
	}

	@Override
	public String toString() {
		return getTitle();
	}

	@Override
	public String toText() {
		TextSerializationBuilder t = new TextSerializationBuilder();
		t.append("id", getId());
		t.append("title", getTitle());
		t.append("description", getDescription());
		t.append("previewImage", getPreviewImage());
		return t.getTextSerialization();
	}

	static class RouteTableDef implements IDataTableDef {

		private static final String TABLE_NAME = "ROUTES";
		private static final List<IDataColumnDef> TABLE_COLUMNS = new ArrayList<IDataColumnDef>();
		static {
			TABLE_COLUMNS.add(new DataColumnDef(0, COLUMN_ROUTE_ID, int.class, true, true));
			TABLE_COLUMNS.add(new DataColumnDef(1, COLUMN_TITLE, String.class));
			TABLE_COLUMNS.add(new DataColumnDef(2, COLUMN_DESCRIPTION, String.class));
			TABLE_COLUMNS.add(new DataColumnDef(3, COLUMN_PREVIEW_IMAGE, String.class));
		}

		@Override
		public String getTableName() {
			return TABLE_NAME;
		}

		@Override
		public List<IDataColumnDef> getColumnsDef() {
			return TABLE_COLUMNS;
		}

		@Override
		public Class<?> getDataRecordType() {
			return User.class;
		}

	}

}
